package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    @Transactional
    public void deleteBatch(Integer[] ids) {
        for (Integer id : ids) {
            if (dishMapper.getStatus(id) == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            if (setmealDishMapper.countDish(id) > 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        dishMapper.deleteBatch(ids);
        dishFlavorMapper.deleteBatch(ids);
    }

    @Override
    public PageResult list(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<DishVO> dishes = dishMapper.selectAll(dto);
        Page page = (Page) dishes;
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void add(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.add(dish);
        Long id = dish.getId();
        log.info("收到主键id：{}",id);
        List<DishFlavor> flavors = dto.getFlavors();
        if(flavors==null||flavors.size()==0)return;
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        dishFlavorMapper.addBatch(flavors);
    }

    @Override
    public DishVO getById(Long id) {
        DishVO vo = dishMapper.selectById(id);
        List<DishFlavor>  flavors = dishFlavorMapper.getByDishId(id);
        vo.setFlavors(flavors);
        return vo;
    }
    @Transactional
    @Override
    public void update(DishDTO dto){
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.update(dish);
        Long id = dto.getId();
        log.info("获得id:{}",id);
        dishFlavorMapper.deleteById(id);
        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dto.getId());
        }
        log.info("flavors:{}",flavors);
        if(flavors!=null && flavors.size()>0){
            dishFlavorMapper.addBatch(flavors);
        }
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
      List<Dish> dishes = dishMapper.getByCategoryId(categoryId);
      return  dishes;
    }

    @Override
    public List<DishItemVO> getListBySetmealId(Long id) {
        List<DishItemVO> list =  dishMapper.getListBySetmealId(id);
        return  list;
    }

    @Override
    public List<DishVO> getDishAndFlavorsByCategoryId(Long categoryId) {

        List<Dish> dishes = dishMapper.getByCategoryId(categoryId);
        List<DishVO> voes =  new ArrayList<>();

        for (Dish dish : dishes) {
            if (dish.getStatus()==0){
                continue;
            }
            DishVO vo = new DishVO();
            BeanUtils.copyProperties(dish,vo);
            List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(dish.getId());
            vo.setFlavors(dishFlavors);
            voes.add(vo);
        }
        return voes;
    }

}
